package inspur.dao;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import inspur.common.ConnectionUtils;
import inspur.entity.BookObject;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import static hyfree.safe.getSHA1.getSHA1;

public class BookDao {
    Connection conn=null;
    PreparedStatement pstm=null;
    ResultSet rs=null;

    /***
     *将指定的书上传到数据库
     * @param mybook
     * flag=0;失败
     * =2；成功
     * =3；失败，原因：重复
     */
    public static int UpLoadBook(BookObject mybook){
        Connection conn=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        //得到数据库对象
        conn= ConnectionUtils.getConnection();
        int flag=0;//注册失败
        try {
            //发送sql文
            String sql="insert  into book(Id,bookname,press,introduction,status) VALUES(?,?,?,?,?)";
            pstm= conn.prepareStatement(sql);
            pstm.setString(1,mybook.getId());
            pstm.setString(2,mybook.getBookName());
            pstm.setString(3,mybook.getPress());
            pstm.setString(4,mybook.getIntroduction());
            pstm.setString(5,mybook.getStatus());
            //rs= pstm.executeQuery();
            int num=pstm.executeUpdate();
            //int num=pstm.executeUpdate();
            //处理结果集
            if (num==1){
                flag=2;
//                关键sha引擎
//                Hashes_fast_engine(mybook);
            }
        } catch (MySQLIntegrityConstraintViolationException e){
                flag=3;
        }catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();


        }finally{

            //释放资源
            ConnectionUtils.close(rs, pstm, conn);
        }
        return flag;
    }

    /**
     * 从数据库移除一本图书
     * @param bookId
     */
    public static int DeleteBook(String bookId){
        Connection conn=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        //得到数据库对象
        conn= ConnectionUtils.getConnection();
        int flag=0;//注册失败
        try {
            //发送sql文
            String sql="DELETE  FROM book WHERE Id=?";
            pstm= conn.prepareStatement(sql);
            pstm.setString(1,bookId);
            //rs= pstm.executeQuery();
            int num=pstm.executeUpdate();
            //int num=pstm.executeUpdate();
            //处理结果集
            if (num==1){
                flag=2;
            }
        } catch (MySQLIntegrityConstraintViolationException e){
            flag=3;
        }catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }finally{

            //释放资源
            ConnectionUtils.close(rs, pstm, conn);
        }
        return flag;
    }

    /**
     *修改一本图书的信息
     * @return
     */
    public static int UpdateBook(String bookId,BookObject _book){
        Connection conn=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        //得到数据库对象
        conn= ConnectionUtils.getConnection();
        int flag=0;//注册失败
        try {
            //发送sql文
            String sql="UPDATE book SET Id=?,bookname=?,press=?,introduction=?,status=?,userid=?,startdate=?,usertime=?,returnbooktime=? WHERE Id=?";
            pstm= conn.prepareStatement(sql);
            pstm.setString(1,_book.getId());
            pstm.setString(2,_book.getBookName());
            pstm.setString(3,_book.getPress());
            pstm.setString(4,_book.getIntroduction());
            pstm.setString(5,_book.getStatus());
            pstm.setString(6,_book.getUserid());
            pstm.setString(7,_book.getStartDate());
            pstm.setString(8,_book.getUserTime());
            pstm.setString(9,_book.getReturnBoookTime());
            pstm.setString(10,bookId);

            //rs= pstm.executeQuery();
            int num=pstm.executeUpdate();
            //int num=pstm.executeUpdate();
            //处理结果集
            if (num==1){
                flag=2;
            }
        } catch (MySQLIntegrityConstraintViolationException e){
            flag=3;
        }catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }finally{

            //释放资源
            ConnectionUtils.close(rs, pstm, conn);
        }
        return flag;
    }

    /**
     * 借书
     * @param bookId
     */
    public static  int BorrowBook(String bookId){
        Connection conn=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        //得到数据库对象
        conn= ConnectionUtils.getConnection();
        int flag=0;//注册失败
        try {
            //发送sql文
            String sql="UPDATE book SET status=0 WHERE Id=?";
            pstm= conn.prepareStatement(sql);
            pstm.setString(1,bookId);
            //rs= pstm.executeQuery();
            int num=pstm.executeUpdate();
            //int num=pstm.executeUpdate();
            //处理结果集
            if (num==1){
                flag=2;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }finally{

            //释放资源
            ConnectionUtils.close(rs, pstm, conn);
        }
        return flag;
    }

    /**
     * 还书
     * @param bookId
     */
    public static   void ReturnBook(String bookId){
        Connection conn=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        //得到数据库对象
        conn= ConnectionUtils.getConnection();
        int flag=0;//注册失败
        try {
            //发送sql文
            String sql="UPDATE book SET status=1 WHERE Id=?";
            pstm= conn.prepareStatement(sql);
            pstm.setString(1,bookId);
            //rs= pstm.executeQuery();
            int num=pstm.executeUpdate();
            //int num=pstm.executeUpdate();
            //处理结果集
            if (num==1){
                flag=2;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }finally{

            //释放资源
            ConnectionUtils.close(rs, pstm, conn);
        }
    }

    /**
     * 哈希快速引擎,用于当管理员上传图书时，建立快速索引表
     * @param mybook
     */
    public void  Hashes_fast_engine(BookObject mybook){
        char[] _bookName=mybook.getBookName().toCharArray();
        char[] _pressName=mybook.getPress().toCharArray();
        char[] _introdoction=mybook.getIntroduction().toCharArray();
        for (char a:_bookName ) {
            Hashes_fast_engine(a,mybook.getId());
        }
        for (char b:_pressName ) {
            Hashes_fast_engine(b,mybook.getId());
        }
        for (char c:_introdoction){
            Hashes_fast_engine(c,mybook.getId());
        }

    }
    public  void  Hashes_fast_engine(char word,String BookId){
        //得到数据库对象
        conn= ConnectionUtils.getConnection();
        int flag=0;//注册失败
        try {
            //发送sql文
            String sql="insert  into hashes_fast_engine(sha1,bookId) VALUES(?,?)";
            pstm= conn.prepareStatement(sql);
            pstm.setString(1,String.valueOf(word));
            pstm.setString(2,BookId);
            //rs= pstm.executeQuery();
            int num=pstm.executeUpdate();
            //int num=pstm.executeUpdate();
            //处理结果集
            if (num==1){
                flag=2;
            }
        } catch (MySQLIntegrityConstraintViolationException e){
            flag=3;
        }catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }finally{

            //释放资源
            ConnectionUtils.close(rs, pstm, conn);
        }
//        return flag;

    }

    /**
     * 寻找指定书名的
     * @param  bookName
     */
    public static ArrayList<BookObject> SelectBookByName(String bookName){
        Connection conn=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        conn=ConnectionUtils.getConnection();
        ArrayList<BookObject> bookArrayList=new ArrayList<BookObject>();
        try {
            //发送sql
            String sql="select * from book where bookname=?";
            pstm=conn.prepareStatement(sql);
            pstm.setString(1,bookName);
            rs=pstm.executeQuery();
            System.out.println(rs);
            while (rs.next()){
                BookObject mybook=new BookObject();
                mybook.setId(rs.getString("Id"));
                mybook.setBookName(rs.getString("bookname"));
                mybook.setPress(rs.getString("press"));
                mybook.setBookImg(rs.getString("bookimg"));
                mybook.setIntroduction(rs.getString("introduction"));
                mybook.setStatus(rs.getString("status"));
                mybook.setUserid(rs.getString("userid"));
                mybook.setStartDate(rs.getString("startdate"));
                mybook.setUserTime(rs.getString("usertime"));
                mybook.setReturnBoookTime(rs.getString("returnbooktime"));
               bookArrayList.add(mybook);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return bookArrayList;

    }
    /**
     * 寻找指定id的书籍
     * @param
     */
    public static BookObject SelectBookById(String id){
        Connection conn=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        conn=ConnectionUtils.getConnection();
        try {
            //发送sql
            String sql="select * from book where Id=?";
            pstm=conn.prepareStatement(sql);
            pstm.setString(1,id);
            rs=pstm.executeQuery();
            while (rs.next()){
                BookObject mybook=new BookObject();
                mybook.setId(rs.getString("Id"));
                mybook.setBookName(rs.getString("bookname"));
                mybook.setPress(rs.getString("press"));
                mybook.setBookImg(rs.getString("bookimg"));
                mybook.setIntroduction(rs.getString("introduction"));
                mybook.setStatus(rs.getString("status"));
                mybook.setUserid(rs.getString("userid"));
                mybook.setStartDate(rs.getString("startdate"));
                mybook.setUserTime(rs.getString("usertime"));
                mybook.setReturnBoookTime(rs.getString("returnbooktime"));
                return mybook;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 寻找指定出版社的书籍
     * @param pressName
     */
    public static ArrayList<BookObject> SelectBookByPress(String pressName){
        Connection conn=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        conn=ConnectionUtils.getConnection();
       ArrayList<BookObject> arrayListBook=new ArrayList<BookObject>();
        try {
            //发送sql
            String sql="select * from book where press=?";
            pstm=conn.prepareStatement(sql);
            pstm.setString(1,pressName);
            rs=pstm.executeQuery();
            System.out.println(rs);
            while (rs.next()){
                BookObject mybook=new BookObject();
                mybook.setId(rs.getString("Id"));
                mybook.setBookName(rs.getString("bookname"));
                mybook.setPress(rs.getString("press"));
                mybook.setBookImg(rs.getString("bookimg"));
                mybook.setIntroduction(rs.getString("introduction"));
                mybook.setStatus(rs.getString("status"));
                mybook.setUserid(rs.getString("userid"));
                mybook.setStartDate(rs.getString("startdate"));
                mybook.setUserTime(rs.getString("usertime"));
                mybook.setReturnBoookTime(rs.getString("returnbooktime"));
                arrayListBook.add(mybook);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return arrayListBook;
    }
    /**
     * 寻找指定关键词的书籍
     * @param  KeyWord
     */
    public static ArrayList<BookObject> SelectBookByKeyWord(String KeyWord){
        Connection conn=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        conn=ConnectionUtils.getConnection();
        ArrayList<BookObject> arrayListBook=new ArrayList<BookObject>();
        try {
            //发送sql
            String sql="select * from book where bookname LIKE ?";
            pstm=conn.prepareStatement(sql);
            pstm.setString(1,"%"+KeyWord+"%");
            rs=pstm.executeQuery();
            System.out.println(rs);
            while (rs.next()){
                BookObject mybook=new BookObject();
                mybook.setId(rs.getString("Id"));
                mybook.setBookName(rs.getString("bookname"));
                mybook.setPress(rs.getString("press"));
                mybook.setBookImg(rs.getString("bookimg"));
                mybook.setIntroduction(rs.getString("introduction"));
                mybook.setStatus(rs.getString("status"));
                mybook.setUserid(rs.getString("userid"));
                mybook.setStartDate(rs.getString("startdate"));
                mybook.setUserTime(rs.getString("usertime"));
                mybook.setReturnBoookTime(rs.getString("returnbooktime"));
                arrayListBook.add(mybook);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return arrayListBook;
    }

    /**
     * 列出全部图书
     * @return
     */
    public static ArrayList<BookObject> SelectAllBook(){
        Connection conn=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        conn=ConnectionUtils.getConnection();
       ArrayList<BookObject> bookArrayList=new ArrayList<BookObject>();
        int idx=0;
        try {
            //发送sql
            String sql="select * from book";
            pstm=conn.prepareStatement(sql);
            rs=pstm.executeQuery();
            System.out.println(rs);
            while (rs.next()){
                BookObject mybook=new BookObject();
                mybook.setId(rs.getString("Id"));
                mybook.setBookName(rs.getString("bookname"));
                mybook.setPress(rs.getString("press"));
                mybook.setBookImg(rs.getString("bookimg"));
                mybook.setIntroduction(rs.getString("introduction"));
                mybook.setStatus(rs.getString("status"));
                mybook.setUserid(rs.getString("userid"));
                mybook.setStartDate(rs.getString("startdate"));
                mybook.setUserTime(rs.getString("usertime"));
                mybook.setReturnBoookTime(rs.getString("returnbooktime"));
               bookArrayList.add(mybook);
            }
            return bookArrayList;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;


    }




}
